#define _CRT_SECURE_NO_WARNINGS
#include "mainwidget.h"

#include <QApplication>

#include "debug.h"
#include "loginwidget.h"
#include "model/datacenter.h"
#include "network/netclient.h"


FILE* output = nullptr;
// 重定向日志
void msgHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
    Q_UNUSED(type);
    Q_UNUSED(context);
    const QByteArray& log = msg.toUtf8();
    fprintf(output, "%s\n", log.constData());
    fflush(output);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

#if DEPOLY
    output = std::fopen("./client.log", "a");
    qInstallMessageHandler(msgHandler);
#endif

#if TEST_SKIP_LOGIN
    MainWidget* w = MainWidget::getInstance();
    w->show();
#else
    LoginWidget* loginWidget = new LoginWidget();
    loginWidget->show();
#endif

    // model::DataCenter* dataCenter = model::DataCenter::getInstance();
    // dataCenter->initDataFile();

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    network::NetClient netClient(dataCenter);
#if TEST_NETWORK
    dataCenter->initDataFile();
    netClient.ping();
#endif
    netClient.initWebsocket();

    return a.exec();
}
